
// 渲染数据
$(function () {
    initEvent();
    getUserInfo();
    getInsuranceInfos();
});

function initEvent(){
    $("#submitMsg").off('click').on('click', function () {
        submitMsg();
    });
}

// 获取用户保险信息
function getInsuranceInfos() {
    var datas = {
        interface:'users@upUserInfo',
        infoType:'4'
    };
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                var result = data.result;
                if (result.hasInsurance == 1 && result.isPass == 1) {
                    $('#insuranceFee').text(result.premium_user);
                    $('#classHour').text(result.user_course_time);
                    $('#startTime').text(result.training_start_time);
                    $('#endTime').text(result.training_end_time);
                    $('#schoolDay').text(result.class_day);
                    $('#insuranceEmail').text(result.email);
                    $('#insuranceSchool').text(result.school);
                } else {
                    $('#insuranceInfo').hide();
                }
            } else if (data.status == '5000') {
                $('#insuranceInfo').hide();
                falert(data.msg);
            }
        }
    });
}

function getUserInfo(){
    var datas = {
        interface:'users@upUserInfo',       
    };
    if(common.get('id')>0){
        datas.b_id = common.get('id');
	$('#repayment_href').attr('href','/order/paymentplan?id='+common.get('id'));
    }
    
    common.ajax({
        data: datas,
        success: function (data) {
            if (data.status == '2000') {
                if(data.result.orderInfo.p_amount_fixed == '1' || !data.result.orderInfo.is_update_order){
                    $('#editAmtBS').remove();
                }
                if(data.result.userInfo.u_true_name){
                    $('#userinfo').html(common.formatTemplate(data.result.userInfo,$('#userinfo').html()));
                }
                if (data.result.orderInfo.b_period) {
                    $('#fenqiinfo').html(common.formatTemplate(data.result.orderInfo, $('#fenqiinfo').html()));
                }
                if (data.result.orderInfo.month_rent_amt) {
                    $('#paymentinfo').html(common.formatTemplate(data.result.orderInfo, $('#paymentinfo').html()));
                }
                if(data.result.orderInfo.p_return_type == 4){
                    $('#paymentinfo').css('height','6rem');
                    $('.xy_month').html('<span class="ban-l fl" style="width:4rem;line-height: 1.2rem;">前'+data.result.orderInfo.p_x_month+'月每月交学费</span><span class="ban-r" style="width: 40%">'+data.result.orderInfo.x_month_pay+'元</span>');
                    $('.xy_month').after('<div class="ban1 line"><span class="ban-l fl" style="width:4rem;line-height: 1.2rem;">后'+data.result.orderInfo.p_y_month+'月每月交学费</span><span class="ban-r" style="width: 40%">'+data.result.orderInfo.y_month_pay+'元</span></div>');
                }
                if(!data.result.userInfo.accountno && data.result.userInfo.is_open){
                    $('#submitMsg').text('去开通电子账户');
                    $("#submitMsg").off('click').on('click', function () {
                        openAccount(data.result.userInfo.is_auth);
                    });
                }else{
                    $("#submitMsg").off('click').on('click', function () {
                        submitMsg();
                    });
                }
            } else if (data.status == '5000') {
                falert(data.msg);
            }
        }
    });
}

//点击提交弹出遮罩层
function openAccount(type) {
    if(type){
        var flag = false;
        var datas = {
            interface:'fund@auth',       
        };
        common.ajax({
            data: datas,
            success: function (data) {
                if(data.status == '2000'){
                    if(data.result.smscode_flag){
                        location.href = '/user/sendcore';
                    }else{
                        flag = true;
                    }
                } else if (data.status == '5000') {
                    falert(data.msg);
                }
            }
        });
        
        if(flag){
            var datas = {
                interface:'fund@open',       
            };
            common.ajax({
                data: datas,
                success: function (data) {
                    if(data.status == '2000'){
                        location.href = data.result.url;
                    } else if (data.status == '5000') {
                        falert(data.msg);
                    }
                }
            });
        }
    }else{
        var datas = {
            interface:'fund@open',       
        };
        common.ajax({
            data: datas,
            success: function (data) {
                if (data.status == '2000') {
                    if(data.result.isJump){
                        document.write(data.result.content);  
                    }else{
                        $('#submitMsg').text('确认并提交');
                        $("#submitMsg").off('click').on('click', function () {
                            submitMsg();
                        });
                        falert('开户成功');
                    }
                    
                } else if (data.status == '5000') {
                    falert(data.msg);
                }
            }
        });
    }
}
//点击提交弹出遮罩层
function submitMsg() {
    if(!$('#checkbox').is(':checked')){
        falert('请勾选并阅读协议');
        return false;
    }
    if($('#editAmtFlag').val() == '1'){
        if (parseFloat($('#b_borrow_amount').val()) <= 0) {
            falert('请输入正确的产品金额');
            return false;
        }
    }
    layer.open({
        type: 1,
        title: false,
        closeBtn: 0,
        skin: 'yourclass',
        btn:['确认提交','暂不提交'],
        success:function(layero){
          layero.find('.layui-layer-btn').css('text-align', 'center');
        },
        shadeClose: false,
        area: ['82%', '250px'],
        content: '确认提交后，将为您生成缴费列表，请保证交费日当天银行卡内余额充足，届时系统会进行自动划扣。',
        btn1: function(index, layero){
            var datas = {
                interface:'order@saveOrder',
                ocr_open_where:'apply_open'
            };
	    if(common.get('id')>0){
	        datas.b_id = common.get('id');
	    }
            if($('#editAmtFlag').val() == '1'){
                datas.b_borrow_amount = $('#b_borrow_amount').val();
            }
            common.ajax({
                data: datas,
                success: function (data) {
                    if (data.status == '2000') {
                        if (data.result.hasInsurance == 1 && data.result.isPass == 1 && data.result.amount > 0) {
                            // 跳转到支付页面
                            location.href = '/pay/?order_num=' + data.result.orderNUm;
                        } else {
                            location.href = '/order/ordersuccess?ucode='+ data.result.u_code;
                        }
                    } else if (data.status == '5000') {
                        falert(data.msg);
                    }
                }
            });
        }
    });
}

function updateAmt(amt,p_min_amt,p_max_amt){
    if($('#editAmtFlag').val() == '1'){
        if (parseFloat($('#b_borrow_amount').val()) <= 0) {
            falert('请输入正确的产品金额');
            return false;
        } else if (parseFloat($('#b_borrow_amount').val()) < p_min_amt) {
            falert('产品金额不能低于最小额度（'+parseInt(p_min_amt)+'）');
            return false;
        } else if (parseFloat($('#b_borrow_amount').val()) > p_max_amt) {
            $("#b_borrow_amount").val(p_max_amt);
            falert('产品金额超出了最大额度（'+parseInt(p_max_amt)+'）');
            return false;
        }
    }
    if(parseFloat($('#b_borrow_amount').val()) !== parseFloat(amt)){
        layer.open({
            type: 1,
            title: false,
            closeBtn: 0,
            skin: 'yourclass',
            btn:['确认修改','暂不修改'],
            success:function(layero){
              layero.find('.layui-layer-btn').css('text-align', 'center');
            },
            shadeClose: false,
            area: ['82%', '250px'],
            content: '确认修改后，产品金额将被修改为您现在输入的金额，您是否确认修改？',
            btn1: function(index, layero){
                var datas = {
                    interface:'order@saveAmount',       
                };
                if(common.get('id')>0){
                    datas.b_id = common.get('id');
                }
                if($('#editAmtFlag').val() == '1'){
                    datas.b_borrow_amount = $('#b_borrow_amount').val();
                }
                common.ajax({
                    data: datas,
                    success: function (data) {
                        if (data.status == '2000') {
                            if(common.get('id')>0){
                                location.href = '/order/jsubmitorder?id='+common.get('id');
                            }else{
                                location.href = '/order/jsubmitorder';
                            }
                        } else if (data.status == '5000') {
                            falert(data.msg);
                        }
                    }
                });
            },
            btn2: function(index, layero){
                $('#editAmtFlag').val('0');
                $('#editAmount').html(amt+'元<a href="javascript:void(0);" onclick="editAmount('+amt+','+p_min_amt+','+p_max_amt+')" style="width:10%;float:right;margin-left:5px;margin-top:2px;"><img src="/static/images/edit25.png"></a>');
            }
        });
    }
}

//修改金额
function editAmount(amt,p_min_amt,p_max_amt) {
    $('#editAmtFlag').val('1');
    $('#editAmount').html('<input class="username fl" name="b_borrow_amount" id="b_borrow_amount" type="tel" placeholder="请输入课程金额" onblur="updateAmt('+parseFloat(amt)+','+parseFloat(p_min_amt)+','+parseFloat(p_max_amt)+')"  maxlength="6" value="'+parseFloat(amt)+'" required style="width:63%;padding-top:0.165rem;">');
}
